/*
   @Copyright:LibreOJ
   @Author:   tjyemail
   @Problem:  https://loj.ac/problem/123
   @Language: C++
   @Datetime: 20-07-30 11:07
   */
// Kruskal - Union-Find-Set, time O(ElogE + V + UFS)

#include <bits/stdc++.h>
using namespace std;

int roots[200005]; // vertex's root
struct Edge {
	int a, b, c; // vertex, cost
	bool operator<(const Edge &e) const { return c>e.c; } // mini-heap
};

inline int find(int leaf)
{
	return leaf==roots[leaf] ? leaf : roots[leaf]=find(roots[leaf]);
}

int main()
{
	int v, e, a, b, c;
	priority_queue<Edge> edges; // mini-heap
	for(scanf("%d %d", &v, &e); e--;) { // v vertexs, e edges
		scanf("%d %d %d", &a, &b, &c); // a->b, cost
		edges.push({a, b, c});
		roots[a]=a;
		roots[b]=b;
	}
	long long cost = 0;
	// must have v-1 edges - kruskal
	for(--v; v && !edges.empty(); edges.pop()) {
		const auto &t = edges.top();
		a = find(t.a), b = find(t.b);
		if (a==b) continue; // have same root ==> circle
		--v;
		roots[b]=a; // join
		cost+=t.c;
	}
	// todo : check it is MST
	printf("%lld\n", cost);
	return 0;
}
